using System;
using System.IO;
using UnityEngine.Networking;

public class Tool
{
    /// <summary>
    /// 倒计时
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public static string ConvertSecondsToTime(int t)
    {
        // 把s转换成TimeSpan对象
        var timespan = TimeSpan.FromSeconds(t);
        // 转换格式"mm':'ss"
        var time = timespan.ToString("mm':'ss");
            
        return time;
    }
}

// 自定义DownloadHandler实现边下边存
public class DownloadHandlerFileStream : DownloadHandlerScript
{
    private readonly FileStream fileStream;

    public DownloadHandlerFileStream(string path, long position) : base(new byte[8192])
    {
        // 未使用FileShare.ReadWrite模式，会导致下载线程与播放线程同时访问文件时发生冲突
        fileStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
        fileStream.Seek(position, SeekOrigin.Begin);
    }

    protected override bool ReceiveData(byte[] data, int dataLength)
    {
        if (data == null || dataLength < 1) return false;
        fileStream.Write(data, 0, dataLength);
        fileStream.Flush();
        return true;
    }

    protected override void CompleteContent()
    {
        fileStream?.Close();
    }
}